<template>
  <el-dialog
    :title="title"
    :visible.sync="visible"
    width="800px"
    :close-on-click-modal="false"
    :modal-append-to-body="false"
    :append-to-body="true"
    :before-close="handleClose"
  >
    <el-form ref="tableEdit" :model="table" :rules="rules" label-width="120px">
      <el-form-item label="表名" prop="TableName">
        <el-input
          v-model="table.TableName"
          :readonly="table.IsMainTable"
          :maxlength="50"
          placeholder="表名"
          :clearable="true"
        />
      </el-form-item>
      <el-form-item label="中文说明" prop="Show">
        <el-input
          v-model="table.Show"
          :maxlength="50"
          placeholder="中文说明"
          :clearable="true"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" style="text-align: center">
      <el-button type="primary" @click="save">保存</el-button>
      <el-button @click="reset">重置</el-button>
    </div>
  </el-dialog>
</template>

<script>
import * as tableApi from '@/customForm/api/phyQueryTable'
export default {
  components: {},
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    formId: {
      type: String,
      default: null
    },
    id: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      title: '新增表',
      table: {},
      rules: {
        TableName: [
          {
            required: true,
            message: '表名不能为空！',
            trigger: 'blur'
          },
          {
            pattern: /^[a-zA-Z_]+$/g,
            message: '表名只能由英文和_组成！',
            trigger: 'blur'
          }
        ],
        Show: [
          {
            required: true,
            message: '中文说明不能为空！',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  watch: {
    visible: {
      handler(val) {
        if (val) {
          this.reset()
        }
      },
      immediate: true
    }
  },
  mounted() {},
  methods: {
    async reset() {
      if (this.id == null) {
        this.title = '添加子表'
        this.table = {
          TableName: null,
          IsMainTable: false,
          Show: null
        }
      } else {
        const res = await tableApi.Get(this.id)
        this.title = '编辑表：' + res.TableName
        this.table = res
      }
    },
    handleClose() {
      this.$emit('update:visible', false)
      this.$emit('close', false)
    },
    async set() {
      const isSet = await tableApi.Set(this.id, {
        TableName: this.table.TableName,
        Show: this.table.Show
      })
      this.$message({
        type: 'success',
        message: '修改成功!'
      })
      this.$emit('update:visible', false)
      this.$emit('close', isSet, this.id)
    },
    async add() {
      const id = await tableApi.Add({
        TableName: this.table.TableName,
        FormId: this.formId,
        Show: this.table.Show
      })
      this.$message({
        type: 'success',
        message: '添加成功!'
      })
      this.$emit('update:visible', false)
      this.$emit('close', true, id)
    },
    save() {
      this.$refs['tableEdit'].validate((valid) => {
        if (valid) {
          if (this.id == null) {
            this.add()
          } else {
            this.set()
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
<style scoped>
.el-table tr {
  line-height: 40px;
}
</style>
